/* ---------------------------------------------------------------------------
   (c) Copyright 2004 ActivCard Corp.
   All Rights Reserved.

   This program is an unpublished copyrighted work which is proprietary
   to ActivCard. This computer program includes Confidential,
   Proprietary Information and is a Trade Secret of ActivCard.
   Any use, disclosure, modification and/or reproduction is prohibited
   unless authorized in writing by ActivCard.

   WARNING:  Unauthorized reproduction of this program as well as
   unauthorized preparation of derivative works based upon the
   program or distribution of copies by sale, rental, lease or
   lending are violations of federal copyright laws and state trade
   secret laws, punishable by civil and criminal penalties.
   --------------------------------------------------------------------------- */

/* ---------------------------------------------------------------------------
   Disclaimer

   ActivCard makes no representations or warranties with respect to the
   contents or use of this software, and specifically disclaims any express
   or implied warranties of merchantability or fitness for any particular
   purpose. Further, ActivCard reserves the right to revise this software
   and to make changes to its content, at any time, without obligation to
   notify any person or entity of such revisions or changes.
   Further, ActivCard makes no representations or warranties with respect to
   any software, and specifically disclaims any express or implied warranties
   of merchantability or fitness for any particular purpose.
   --------------------------------------------------------------------------- */

#ifndef _AKTOOLS_H_

#define _AKTOOLS_H_

#include "acm.h"

#ifdef	__cplusplus 
extern "C" {
#endif



#define CHAR_HEX(c) (((c)>='0' && (c)<='9') ? ((c) - '0') : ( ((c)>='a' && (c)<='f') ? ((c)-'a'+0x0a) : ((c)-'A'+0x0a) ))
#define HEX_CHAR(c) (((c)>=0 && (c)<=9) ? ((c) + '0') : ((c)+'A'-0x0a) )

#define AK_CERT_NB				ACM_CERT_NB
#define AK_EXT_CERT_NB			ACM_EXT_CERT_NB
#define AK_CERT_FIELD_SIZE		ACM_CERT_FIELD_SIZE
#define AK_DEFAULT_PADDING_CHAR	15  /* corresponding to F */
#endif

#define AK_TOOL_SHIFT_LEFT		1
#define AK_TOOL_SHIFT_RIGHT		2

typedef struct
{
  char	szDATA[AK_EXT_CERT_NB][AK_CERT_FIELD_SIZE];
  int	nLEN_MAX[AK_EXT_CERT_NB];
  unsigned int unNbField;			/* Nb effective field */
  unsigned char ucPadd;				/* padding character */
} certif_data;

/* time since the 1/1/1995 */
#define TIME_1_1_95  788918400UL



/* ------ Functions  Prototypes ---------------------------- */

void akiComputeDiffTime (
	const char *pszActivCardClock,
	unsigned long ulClock, 
	int *pnOffsetToken);

unsigned long akiGetActivCardTime (
	unsigned long ulClock);

int akiIsFormat (
	int nFormat);

int akiIsCheckDigit (
	int nCheckDigit);

int akiIsString(
	const char *pszString,
	int nMaxSize);

int akiIsStringHex (
	const char *pszString);

int akiIsValidDeriv (
	const int nDerivType);

int akiIsDesKey (
	const char *pszKey);

int akiIsStringNum (
	const char *pszString
	);

int akiIsValidDec(
	const char *pszData,
	char cDecSep);

int akiIsValidNumValue (
	const char *pszValue);

int akiIsValidResponse (
	const char *pszResponse,
	int nFormat);

int akiIsValidRespLen (
	const char *pszResponse,
	int nRespLen);

/*
int akiIsStandardRespLen (
	int nRespLen);
*/

int akiIsValidClockExtract (
	int nClockExtract);

int akiIsValidData (
	const char *pszString,
	char cDecSep);       

int akiAddCd (
	char *pszChallenge,
	int nChalCd,
	int nAuthType);

int akiControlCheckDigit (
	const char *pszStrInput,
	char *pszStrOutput,
	int nCheckDigit);

int akiGetCheckDigit (
	char *pszChallenge,
	int nLenChallenge);

void akiConvBinFormatDec (
	unsigned char *pszValue,
	int nLengthValue);

void akiConvAsciiFormatDec(
	char *pszIn);

int akiSynchronizeKey (
	char *pszKey,
	unsigned int ulCounter,
	const char *pszResponse,
	int nDerivType			/* Key Derivation type */);

void akiComputeInternalChallenge (
	unsigned char *pszChallenge,
	unsigned long ulClock,
	unsigned int *pCounter,
	int nCT,
	int nCLK,
	int nExtractClock,
	int *pnOffsetClock,
	int nCntInc);

int akiFormatDataMessage(	
	const certif_data *stDataIn,
	char cDecSep,					
    unsigned char *pszBinOut);			

int akiIsValidDesType (
	char cDesType);

int akiIsCorrectDesKey(
	char cDesType, 
	const char *pszKey);       

int akiIsValidCounterKey(
	int nSyncKDrv, 
	int nSyncCInc);       

int akiComputeLuhnCheckDigit(
	char *szBuf);

int akiAddLuhnCheckDigit(
	char *szBuf, 
	int nCheckDigit);

int akiToolOrBits(unsigned char *ucBuf, 
				   unsigned int unBufSize, 
				   unsigned char *ucAddBuf, 
				   unsigned int unAddBufSize, 
				   unsigned int unBits);

int akiToolAddBits(unsigned char *ucBuf, 
				   unsigned int unBufSize, 
				   unsigned char *ucAddBuf, 
				   unsigned int unAddBufSize, 
				   unsigned int unBits);

int akiToolShiftBits(unsigned char *ucBuf, 
					 unsigned int unBufSize, 
					 unsigned int unShiftBits, 
					 unsigned int  dir);

int akiToolBcd_str_Bin_str_conv( unsigned char *data,  
						 int datalen , 
						 unsigned char *output, 
						 int outlen);

int akiGetBitCount( unsigned char *buf, int bufSize, int *noBits);

unsigned int akiGetExponent(int base, int power);

/* --------------------------------------------------------- */

#ifdef	__cplusplus
}

#endif /* _AKTOOLS_H_ */
